package com.boco.blch.analysiser.compliance.HW.manager;

import com.boco.blch.analysiser.compliance.ComplianceResolved;
import com.boco.blch.analysiser.util.XmlUtil;

/**
 * YARN&MR用户访问权限控制安全基线要求项   【控制节点】
 * 【控制节点路径】：
 * 1、查看/opt/huawei/bigdata/etc/2_21_ResourceManager/yarn-site.xml的
 * 配置项yarn.resourcemanager.scheduler.class的值为：
 * org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler
 * 
 * 2、查看/opt/huawei/bigdata/etc/2_21_ResourceManager/capacity-scheduler.xml 的
 * 配置项yarn.scheduler.capacity.root.queues的值，
 * 如：QueueA,QueueB   --以逗号分隔；
 * 
 * 3、分别以 第二步的队列，做如下确认
 * A、yarn.scheduler.capacity.root.QueueA.state的值是RUNNING 或 STOPPED；
 * B、yarn.scheduler.capacity.root.QueueA.acl_submit_applications的值不为空；
 * C、yarn.scheduler.capacity.root..QueueA.acl_administer_queue的值不为空；
 * @author admin
 *
 */
public class BOCO_MR_VisitCtl_UserVisitCtl implements ComplianceResolved {
	
	private final static String yarn_name = "yarn.resourcemanager.scheduler.class";
	private final static String yarn_value = "org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler";
	private final static String capacity_name = "yarn.scheduler.capacity.root.queues";
	private final static String state_name_1 = "RUNNING";
	private final static String state_name_2 = "STOPPED";

	@Override
	public int realEchoResolved(String echoResult) {
		int result = FAILED;
		if(echoResult != null && echoResult.length() > 0){
			if(result == FAILED && echoResult.indexOf("ResourceManager-start") != -1){//控制节点
				String strResult = echoResult.substring(echoResult.indexOf("ResourceManager-start")+"ResourceManager-start".length(), echoResult.indexOf("ResourceManager-end")).trim();
				result = getResult(strResult);
			}
			if(result == FAILED && echoResult.indexOf("JobHistoryServer-start") != -1){//控制节点
				String strResult = echoResult.substring(echoResult.indexOf("JobHistoryServer-start")+"JobHistoryServer-start".length(), echoResult.indexOf("JobHistoryServer-end")).trim();
				result = getResult(strResult);
			}
		}
		return result;
	}
	
	/**
	 * @param echoResult
	 * @return
	 */
	private int getResult(String echoResult){
		int result = FAILED;
		if(echoResult != null && echoResult.indexOf("yarn-site-start") != -1){//yarn-site.xml解析
			String str1 = echoResult.substring(echoResult.indexOf("yarn-site-start")+"yarn-site-start".length(), echoResult.indexOf("yarn-site-end"));
			String value = "";
			if(str1 != null && str1.length() > 0){//.contains(NotFindFile)){//目录不存在
//				result = SUCCESS;
//			}else{
				value = XmlUtil.getValueByName(str1, yarn_name).trim();
				if(value != null && value.equals(yarn_value)){
					result = SUCCESS;
				}else{
					result = FAILED;
				}
			}
			if(result == SUCCESS){
				//capacity-scheduler.xml 的配置项yarn.scheduler.capacity.root.queues的值，
				String str2 = echoResult.substring(echoResult.indexOf("capacity-scheduler-start")+"capacity-scheduler-start".length(), echoResult.indexOf("capacity-scheduler-end"));
				if(str2 != null && str2.length() > 0){//.contains(NotFindFile)){//目录不存在
//					result = SUCCESS;
//				}else{
					value = XmlUtil.getValueByName(str2, capacity_name).trim();//QueueA,QueueB
					//System.out.println("value------"+value);
					if(value != null && value.length() > 0){
						if(value.indexOf(",") != -1){
							String []strValue = value.split(",");
							for (int i = 0; i < strValue.length; i++) {
								result = checkResult(str2, strValue[i].trim());
								if(result == FAILED){
									break;
								}
							}
						}else{
							result = checkResult(str2, value);
						}
					}else{//capacity-scheduler.xml相关配置项没有值
						result = FAILED;
					}
				}
			}
		}
		return result;
	}
	
	private int checkResult(String echoResult,String strValue){
		//System.out.println("echoResult-----------:"+echoResult);
		//System.out.println("strValue-------:"+strValue);
		int result = FAILED;
		if(strValue != null && strValue.length() >0){
			String name_A = "yarn.scheduler.capacity.root."+strValue+".state";
			String name_B = "yarn.scheduler.capacity.root."+strValue+".acl_submit_applications";
			String name_C = "yarn.scheduler.capacity.root."+strValue+".acl_administer_queue";
			String value_A = XmlUtil.getValueByName(echoResult, name_A).trim();
			String value_B = XmlUtil.getValueByName(echoResult, name_B).trim();
			String value_C = XmlUtil.getValueByName(echoResult, name_C).trim();
			
			//System.out.println("value_A----:"+value_A);
			//System.out.println("value_B-----:"+value_B);
			//System.out.println("value_C-----:"+value_C);
			
			if(value_A != null && (value_A.equals(state_name_1) || (value_A.equals(state_name_2)))
					&& value_B != null && value_B.length() > 0 && value_C != null && value_C.length()>0){
				//System.out.println("1111111------------");
				result = SUCCESS;
			}else{
				//System.out.println("22222---------------");
				result = FAILED;
			}
		}
		return result;
	}
	
	


}
